﻿using ServiceStack.Redis.Extend;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Collections;
using ServiceStack.Redis;

namespace ServiceStack.Redis.Extend.Test
{
    
    
    /// <summary>
    ///这是 RedisClientTest 的测试类，旨在
    ///包含所有 RedisClientTest 单元测试
    ///</summary>
    [TestClass()]
    public class RedisClientTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        private static RedisClient mClient = new RedisClient();


        private static User GetUser()
        {
            User user = new User();
            user.Address = "longdong";
            user.UserName = Guid.NewGuid().ToString("N");
            user.Region = "guangdong";
            user.City = "guangzhou";
            user.Birthday = DateTime.Now;
            user.Email = user.UserName + "@msn.com";
            return user;

        }

        /// <summary>
        ///AddToList 的测试
        ///</summary>
        [TestMethod()]
        public void AddToListTest()
        {
            User user = GetUser();
            mClient.AddToList("users",user);
            int count = mClient.Count("users");
            User data = mClient.GetItemFromList<User>("users", count - 1);
            Assert.AreEqual(user.UserName, data.UserName);
        }


        /// <summary>
        ///Count 的测试
        ///</summary>
        [TestMethod()]
        public void CountTest()
        {
            int count=  mClient.Count("users");
            Assert.IsTrue(count > 0);
        }

        /// <summary>
        ///EnqueueToList 的测试
        ///</summary>
        [TestMethod()]
        public void EnqueueToListTest()
        {
            User user = GetUser();
            mClient.EnqueueToList("users", user);
            User result = mClient.List<User>("users", 0, 1)[0];
            Assert.IsTrue(user.UserName == result.UserName);
        }

        /// <summary>
        ///Get 的测试
        ///</summary>
        
        public void GetTestHelper<T>()
            where T : class , new()
        {
            T user = mClient.Get<T>("henry");
            Assert.IsTrue(user !=null);
        }

        [TestMethod()]
        public void GetTest()
        {
            GetTestHelper<User>();
        }

        /// <summary>
        ///Get 的测试
        ///</summary>
        [TestMethod()]
        public void GetTest1()
        {
            User user = (User)mClient.Get("henry", typeof(User));
            Assert.IsTrue(user != null);
        }

        /// <summary>
        ///GetJson 的测试
        ///</summary>
        [TestMethod()]
        public void GetJsonTest()
        {
            string value = mClient.GetJson("henry");
            Assert.IsTrue(value != null);
        }

        /// <summary>
        ///List 的测试
        ///</summary>
        public void ListTestHelper<T>()
            where T : class , new()
        {
            IList<User> users = mClient.List<User>("users");
            Assert.IsTrue(users.Count > 0);
        }

        [TestMethod()]
        public void ListTest()
        {
            ListTestHelper<User>();
        }

        /// <summary>
        ///List 的测试
        ///</summary>
        [TestMethod()]
        public void ListTest1()
        {
            IList list = mClient.List("users", typeof(User));
            Assert.IsTrue(list.Count > 0);
        }

        /// <summary>
        ///List 的测试
        ///</summary>
        [TestMethod()]
        public void ListTest2()
        {
            IList list = mClient.List("users", 0,10,typeof(User));
            Assert.IsTrue(list.Count > 0);
        }

        /// <summary>
        ///List 的测试
        ///</summary>
        public void ListTest3Helper<T>()
        {
            IList<T> list = mClient.List<T>("users", 0, 10);
            Assert.IsTrue(list.Count > 0);
        }

        [TestMethod()]
        public void ListTest3()
        {
            ListTest3Helper<User>();
        }

        /// <summary>
        ///ListToJson 的测试
        ///</summary>
        [TestMethod()]
        public void ListToJsonTest()
        {
            string json = mClient.ListToJson("users",0,10);
            Assert.IsTrue(!string.IsNullOrEmpty(json));
        }

        /// <summary>
        ///ListToJson 的测试
        ///</summary>
        [TestMethod()]
        public void ListToJsonTest1()
        {
            string json = mClient.ListToJson("users");
            Assert.IsTrue(!string.IsNullOrEmpty(json));
        }

        /// <summary>
        ///ReadAction 的测试
        ///</summary>
        [TestMethod()]
        public void ReadActionTest()
        {
            mClient.ReadAction(c =>
            {
                User user = c.Get<User>("henry");
                Assert.IsTrue(user != null);
            });
        }

        /// <summary>
        ///Set 的测试
        ///</summary>
        [TestMethod()]
        public void SetTest()
        {
            User user = GetUser();
            mClient.Set("henry", user);
            User item = mClient.Get<User>("henry");
            Assert.AreEqual(user.UserName, item.UserName);

        }

        /// <summary>
        ///WriteAction 的测试
        ///</summary>
        [TestMethod()]
        public void WriteActionTest()
        {
            mClient.WriteAction(c => {
                User user = GetUser();
                c.Set("henry", user);
                User item = c.Get<User>("henry");
                Assert.AreEqual(user.UserName, item.UserName);
            });
        }

        /// <summary>
        ///ClientManager 的测试
        ///</summary>
        [TestMethod()]
        public void ClientManagerTest()
        {
           
        }
    }
}
